#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/7/15 13:51
# @Author  : CoderCharm
# @File    : views.py
# @Software: PyCharm
# @Github  : github/CoderCharm
# @Email   : wg_python@163.com
# @Desc    :
"""

用户视图

"""
from api.common import deps
from fastapi import APIRouter, Depends
from flask_restful import reqparse
from sqlalchemy.orm import Session
from api.utils import response_code
import re
router = APIRouter()
import re



@router.get("/test", summary="测试")
async def test(db: Session =Depends(deps.get_db),mobile:str='',code:int=0):
    if not all([mobile,code]):
        return response_code.resp_1001(message="必须输入")
    res = re.match("^1[3-9]\d{9}$",mobile)
    if not res:
        return response_code.resp_1002(message="手机号格式不正确")
    # 手机号码是否在数据库
    return response_code.resp_200(message="手机号合法" )






# 登录/注册
# class UserView(Resource):
#     def get(self):
#         return '11'
#
#     def post(self, db=None, redis=None):
#         req = reqparse.RequestParser()
#         req.add_argument('mobile', required=True)
#         req.add_argument('sms_code', required=True)
#         args = req.parse_args()
#
#         # 判断用户是否存在
#         user_info = UserModel.query.filter(UserModel.mobile == args['mobile']).first()
#         if not user_info:
#             # 注册
#             # 判断验证码是否正确
#             r = redis.Redis(host='127.0.0.1', port=6379)
#             sms_code = r.get(args['mobile'])
#
#             if not sms_code:
#                 return jsonify({'code': 400, 'msg': '验证码已过期'})
#             if sms_code.decode() != args['sms_code']:
#                 return jsonify({'code': 400, 'msg': '验证码不一致'})
#
#             db.session.add(UserModel(
#                 mobile=args['mobile']
#             ))
#             db.session.commit()
#
#         # 登录
#         user_info = UserModel.query.filter(UserModel.mobile == args['mobile']).first()
#         token = jwt.encode({
#             'id': user_info.id,
#             'mobile': user_info.mobile,
#         }, '1')
#
#         return jsonify({
#             'code': 200,
#             'msg': '登录成功',
#             'token': token
#         })
#
#
#
# api.add_resource(UserView, '/user')












